package secondriver.heathfood.bean;

import android.os.Parcel;
import android.os.Parcelable;
import com.alibaba.fastjson.JSONObject;

/**
 * Author : secondriver
 * Date :  2015/11/2
 */
public class ApkInfoEntity implements Parcelable {

    public static final String APK_INFO_FILE_NAME = "appversion.json";
    public static final String APK_INFO_KEY = "apk_info_key";

    public String versionName;
    public int versionCode;
    public String appUrl;

    /**
     * {
     * "versionName": "HeathFood v10",
     * "versionCode": 10,
     * "appUrl": "http://download.app.com/heathfood-v10.apk"
     * }
     *
     * @param json
     * @return
     */
    public static ApkInfoEntity builder(String json) {
        return JSONObject.parseObject(json, ApkInfoEntity.class);
    }

    public String getApkName() {
        int index = appUrl.lastIndexOf("/");
        if (index != -1) {
            return appUrl.substring(index + 1);
        } else {
            return "heathfood-v1.2.apk";
        }
    }

    @Override
    public String toString() {
        return "ApkInfoEntity{" +
                "versionName='" + versionName + '\'' +
                ", versionCode=" + versionCode +
                ", appUrl='" + appUrl + '\'' +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(versionCode);
        dest.writeString(versionName);
        dest.writeString(appUrl);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator<ApkInfoEntity>() {

        @Override
        public ApkInfoEntity createFromParcel(Parcel source) {
            ApkInfoEntity apkInfoEntity = new ApkInfoEntity();
            apkInfoEntity.versionCode = source.readInt();
            apkInfoEntity.versionName = source.readString();
            apkInfoEntity.appUrl = source.readString();
            return apkInfoEntity;
        }

        @Override
        public ApkInfoEntity[] newArray(int size) {
            return new ApkInfoEntity[size];
        }

    };
}
